﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Oracle.DataAccess.Client;

namespace TP2
{
    public partial class LoanForm : Form
    {
        int id_client;
        string client_type;
        
        public LoanForm(int idClient, string clientType)
        {
            id_client = idClient;
            client_type = clientType;
            InitializeComponent();
        }

        private void ClearList()
        {
        }

        public void RefreshTable()
        {
            DBWorkClass dbWork = new DBWorkClass();
            LoanTypeClass selectMass = new LoanTypeClass();
            selectMass = dbWork.SelectFromLoan(client_type, id_client);

            if (selectMass.number.Count == 0) 
            {
                dataGridView1.RowCount = 1; 
                dataGridView1[0, 0].Value = ""; 
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
                dataGridView1[3, 0].Value = "";
                dataGridView1[4, 0].Value = "";
            }
            else { dataGridView1.RowCount = selectMass.number.Count; }
            int i = 0;
            while (i < selectMass.number.Count)
            {
                dataGridView1[0, i].Value = selectMass.number[i].ToString();
                dataGridView1[1, i].Value = selectMass.procent[i].ToString();
                dataGridView1[2, i].Value = selectMass.start_date[i].ToString();
                dataGridView1[3, i].Value = selectMass.finish_date[i].ToString();
                if (Convert.ToInt32(selectMass.credit[i]) == 0) 
                {
                    dataGridView1[4, i].Value = "погашен";
                }
                else
                { dataGridView1[4, i].Value = selectMass.credit[i].ToString(); }
                dataGridView1[5, i].Value = selectMass.payment[i].ToString();
                i++;
            }

        }


        private void LoanForm_Load(object sender, EventArgs e)
        {
            this.RefreshTable();
            dataGridView1.ClearSelection();
        }

        private void buttonAddCredit_Click(object sender, EventArgs e)
        {
            AddLoanForm loanForm = new AddLoanForm(client_type, this, id_client);
            loanForm.Show();
        }

        private void buttonEditCredits_Click(object sender, EventArgs e)
        {
            DBWorkClass dbWork = new DBWorkClass();
            LoanTypeClass selectMass = new LoanTypeClass();
            selectMass = dbWork.SelectFromLoan(client_type, id_client);
            int index=dataGridView1.CurrentRow.Index;
            int num = Convert.ToInt32(selectMass.number[index]);
            int proc = Convert.ToInt32(selectMass.procent[index]);
            EditCreditForm editForm = new EditCreditForm(id_client, num, proc, client_type, Convert.ToDateTime(selectMass.finish_date[index]), Convert.ToInt32(selectMass.credit[index]), this);
            editForm.Show();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            buttonEditCredits.Enabled = true;
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
